hostip = window.location.host;

hostip = "192.168.4.1";

 var lockReconnect = false; //重连锁，避免重复连接

wsUri = "ws://10.10.10.100/ws"
wsUri = wsUri.replace("10.10.10.100", hostip)
var websocket;
StartWebSocket(wsUri)
method = {
	"TT": 0.1,
	"ST0": 0.1,
	"RH": 1,
	"PP": 0.1,
	"WS": 0.1,
	"RF": 0.1,
	"AT": 0.1,
	"DC": 0.1,

}
jsondata = {};
lasttime = new Date();
console.log(lasttime)

function StartWebSocket(wsUri) {
	websocket = new WebSocket(wsUri);
	websocket.onopen = function(evt) {
		onOpen(evt)
	};
	websocket.onclose = function(evt) {
		onClose(evt)
	};
	websocket.onmessage = function(evt) {
		onMessage(evt)
	};
	websocket.onerror = function(evt) {
		onError(evt)
	};
}


function onOpen(evt) {
	console.log("状态:已连接")

	$('#Networkstatus').attr("class", "label label-success");
	$('#Networkstatus').html("正常");
	socket_status = 1;
	//		$("#state").html("状态:已连接")
	//		// writeToScreen("<span style='color:red'>连接成功，现在你可以发送信息啦！！！</span>");
}

function onClose(evt) {
	// writeToScreen("<span style='color:red'>websocket连接已断开!!!</span>");
	//		$("#state").html("状态:已断开")
	console.log("状态:已断开")
	$('#Networkstatus').attr("class", "label label-danger");
	$('#Networkstatus').html("异常");
	socket_status = 0;
	websocket.close();
}

function onMessage(evt) {
	// writeToScreen(evt.data);


	nowtime = new Date();
	if (nowtime - lasttime > 1000)
		recdata = evt.data;
	else
		recdata = recdata + evt.data;

	lasttime = new Date();
	if (recdata.indexOf("[") == -1 || recdata.indexOf("]") == -1)
		return;

	recdata = recdata.replace("[", "")
	recdata = recdata.replace("]\r\n", "")
	// console.log(recdata)
	temp1 = recdata.split(",");
	jsondata = {};
	jsoneach(temp1, function(value, index) {
		// temp2 = value.split(" ");
		// jsondata[temp2[0]]=temp2[1]

		temp2 = value.split(" ");
		nametemp = temp2[0];

		if (method.hasOwnProperty(nametemp)) {
			tempval = parseInt(temp2[1]) * method[nametemp]
			tempval = tempval.toFixed(1);
		} else
			tempval = temp2[1];
		// jsondata[temp2[0]]=temp2[1]
		jsondata[nametemp] = tempval

	});
	getdatashow();
}


function doSend() {
	var message = $("#message").val();
	message = message.replace(/\n/g, "\r\n");

	if (message == '') {
		alert("请先填写发送信息");
		$("#message").focus();
		return false;
	}
	if (typeof websocket === "undefined") {
		alert("还没有连接，或者连接失败，请检测");
		return false;
	}
	if (websocket.readyState == 3) {
		alert("已经关闭，请重新连接");
		return false;
	}
	console.log(websocket);

	websocket.send(message);
}

function isArrayLike(obj) {
	if (Object.prototype.toString.call(obj) == '[object Array]') {
		return true;
	}
	var length = 'length' in obj && obj.length;
	return typeof length === 'number' && length >= 0;
}

function jsoneach(arr, callback) {
	if (isArrayLike(arr)) {
		for (var i = 0; i < arr.length; i++) {
			if (callback.call(arr[i], arr[i], i) === false) break;
		}
	} else {
		for (var k in arr) {
			if (callback.call(arr[k], arr[k], k) === false) break;
		}
	}
	return arr;
}

function reconnect(url) {
	if (lockReconnect) return;
	lockReconnect = true;
	//没连接上会一直重连，设置延迟避免请求过多
	setTimeout(function() {
		StartWebSocket(url);
		lockReconnect = false;
		console.log("尝试重连");
	}, 2000);
}
function onError(evt) {
	console.log("状态:连接异常")

	$('#Networkstatus').attr("class", "label label-danger");
	$('#Networkstatus').html("异常");
	socket_status = 0;
	reconnect(wsUri);
	//		$("#state").html("状态:已连接")
	//		// writeToScreen("<span style='color:red'>连接成功，现在你可以发送信息啦！！！</span>");
}